package org.myself.meeting.controller;


import org.myself.meeting.Utils.Constant;
import org.myself.meeting.model.*;
import org.myself.meeting.service.DepartmentService;
import org.myself.meeting.service.EmployeeService;
import org.myself.meeting.service.MeetingRoomService;
import org.myself.meeting.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MeetingController {

    @Autowired
    MeetingRoomService mrs;

    @Autowired
    DepartmentService ds;

    @Autowired
    EmployeeService es;

    @Autowired
    MeetingService mts;

    //预定会议室
    @RequestMapping("/bookmeeting")
    @ResponseBody
    public Result bookMeeting(@RequestBody Base base){
        return new Result(200,"success",mrs.getAllmrsOutStatus(Constant.STATUS));
    }

    //展示所有的部门名称
    @RequestMapping("/alldeps")
    @ResponseBody
    public Result getAllDeps(@RequestBody Base base){
        return new Result(200,"success",ds.getAllDeps());
    }

    //根据部门获取对应的员工
    @RequestMapping("/getempsbydepid")
    @ResponseBody
    public Result getEmpsByDepId(@RequestBody GetempsBydepid acc){
        return new Result(200,"success",es.getEmpsByDepId(acc.getDepId()));
    }

    //添加预定会议室的表单
    @RequestMapping("/doaddmeeting")
    @ResponseBody
    public Result addMeeting(@RequestBody Doaddmeeting acc){
        if(acc.getMps().length==0){
            return new Result(400,"fail",null);
        }
        Employee emp =es.getEmpsByUsername(acc.getUsername()) ;
        //如果是经理级别的  直接通过
        if(emp.getRole() == 2){
            acc.getMeeting().setStatus(Constant.NORMAlSTATUS);
        }else{  //需要审核
            acc.getMeeting().setStatus(Constant.TRIALSTATUS);
        }
        //发起者
        acc.getMeeting().setReservationistid(emp.getEmployeeid());
        //需要能用的会议室
        Integer result = mts.addMeeting(acc.getMeeting(),acc.getMps(),emp);
        return new Result(200,"success",null);

    }

    //搜索会议
    @RequestMapping("/searchmeetings")
    @ResponseBody
    public Result searchmeetings(@RequestBody Searchmeetingbyname acc){
//        System.out.println(meetingname);

        long total ;
        List<SearchMeeting> list;
        if(acc.getMeetingname()==null||acc.getMeetingname().equals("")){
            list = mts.getAllmeetings(acc.getPages(), Constant.PAGESIZE, Constant.NORMAlSTATUS);
            total = mts.getTotal(Constant.NORMAlSTATUS);
        }else {
            list = mts.getMeetingsByName(acc.getPages(), Constant.PAGESIZE, Constant.NORMAlSTATUS,acc.getMeetingname());
            total = mts.getNameTotal(Constant.NORMAlSTATUS,acc.getMeetingname());
        }
        Map<String,Object> map = new HashMap<String, Object>();

        map.put("meetings",list);
        map.put("total",total);
        map.put("page",acc.getPages());
        map.put("pagenum",total%Constant.PAGESIZE==0?total/Constant.PAGESIZE : total/Constant.PAGESIZE+1 );
        return new Result(200,"success",map);
    }

    //查看详情
    @RequestMapping("/meetingdetails")
    @ResponseBody
    public Result meetingDetails(@RequestBody Meetingdetails acc){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("meeting",mts.getMetByIdSTATUS(acc.getId(),Constant.NORMAlSTATUS));
        map.put("people",mts.getMetPerById(acc.getId()));
        return new Result(200,"success",map);
    }

    @RequestMapping("/meeting/room/delete")
    @ResponseBody
    public Result DelMeeting(@RequestBody DelMeetingRoom acc){
        mts.deletemeeting(acc.getRoomid());
        return new Result(200,"success",null);
    }
}
